require(['./config'], () => {
    require(['template', 'header', 'footer'], (template) => {

        class List {
            constructor() {
                this.getList();
                this.gotoDetail();
                this.changePage();
            }

            //获取数据
            getList(count = 0) {
                const id = location.search.slice(4);
                $.get(`http://www.xiongmaoyouxuan.com/api/tab/${id}?start=${count}`, (resp) => {
                    console.log(resp.data.items.list)
                    if (resp.code === 200) {
                        const list = resp.data.items.list;
                        const {cname} = resp.data.category;
                        $('#goodsClassify').html(template('goodsClassifyTemplate', {list}));
                        $('#wrap').html(template('libsTemplate', {cname}));
                        this.sortSell(list);
                        this.sortPrice(list);
                        this.sortDate(list);
                    }
                });

            }


            //点击分页
            changePage() {
                let pageNum = 0;
                $('#nextPage').on('click', () => {
                        pageNum++;
                        if (pageNum >= 9) {
                            pageNum = 9
                        }
                        let nowPage = pageNum * 20;
                        this.getList(nowPage);
                        $('#nowPageNum').html(`当前为第 ${pageNum + 1} 页`);
                    }
                )
                $('#beforePage').on('click', () => {
                        pageNum--;
                        if (pageNum <= 0) {
                            pageNum = 0
                        }
                        let nowPage = pageNum * 20;
                        this.getList(nowPage);
                        $('#nowPageNum').html(`当前为第 ${pageNum + 1} 页`);
                    }
                )
            }

            //销量排序
            sortSell(list) {
                let flag = true;
                $('#sell').on('click', function(){
                    flag = !flag;
                    if (flag) {
                        $(this).html('销量升序')
                        list = list.sort(function (a, b) {
                            return b.saleNum - a.saleNum
                        });
                    }else {
                        $(this).html('销量降序')
                        list = list.sort(function (a, b) {
                            return a.saleNum - b.saleNum
                        });
                    }
                    $('#goodsClassify').html(template('goodsClassifyTemplate', {list}));
                })
            }

            //价格排序
            sortPrice(list){
                let flag = true;
                $('#price').on('click', function(){
                    flag = !flag;
                    if (flag) {
                        $(this).html('价格升序')
                        list = list.sort(function (a, b) {
                            return b.price - a.price
                        });
                    }else {
                        $(this).html('价格降序')
                        list = list.sort(function (a, b) {
                            return a.price - b.price
                        });
                    }
                    $('#goodsClassify').html(template('goodsClassifyTemplate', {list}));
                })
            }

            //时间排序
            sortDate(list){
                let flag = true;
                $('#time').on('click', function(){
                    flag = !flag;
                    if (flag) {
                        $(this).html('时间升序')
                        list = list.sort(function (a, b) {

                            return new Date(b.expireDate).getDate() - new Date(a.expireDate).getDate();
                        });
                    }else {
                        $(this).html('时间降序')
                        list = list.sort(function (a, b) {
                            return new Date(a.expireDate).getDate() - new Date(b.expireDate).getDate();
                        });
                    }
                    $('#goodsClassify').html(template('goodsClassifyTemplate', {list}));
                })
            }




            gotoDetail() {
                const pid = location.search.slice(4);
                $('#goodsClassify').on('click', 'li', function () {
                    const id = $(this).attr('data-pid');
                    let tempwindow = window.open('_blank');
                    tempwindow.location = `/html/detail.html?id=${id}#pid=${pid}`;
                })
            }
        }

        new List();
    })
})



